package com.qianfeng.day11.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.day11.entity.*;
import com.qianfeng.day11.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class MobileController {
    @Resource
    private MobileTypeService mobileTypeService;
    @Resource
    private PhoneService phoneService;
    @Resource
    private BasicSituationService basicSituationService;
    @Resource
    private FunctionalSituationService functionalSituationService;
    @Resource
    private WearDegreeService wearDegreeService;
    @Resource
    private PriceService priceService;

    @RequestMapping("/home")
    public String home(Model model,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       Integer typeId, String key){
        List<MobileType> typeList = mobileTypeService.findAll();
        model.addAttribute("typeList", typeList);
        PageHelper.startPage(page,8);// 设置分页信息（第几页，每页显示几条）
        List<Phone> list = phoneService.findAll(typeId, key);
        PageInfo pageInfo = new PageInfo(list); // 将查询结果封装成pageBean
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("typeId", typeId);
        model.addAttribute("key", key);
        return "/home";
    }

    @RequestMapping("/info")
    public String info(Model model, Integer id){
        Phone phone = phoneService.findById(id); // 手机信息
        model.addAttribute("phone", phone);
        // 第一步信息
        List<BasicSituation> bsList = basicSituationService.findAllByPhoneId(id);
        model.addAttribute("firstList", bsList);
        // 第二步信息
        List<FunctionalSituation> fsList = functionalSituationService.findAllByPhoneId(id);
        model.addAttribute("secondList", fsList);
        // 第三步信息
        List<WearDegree> wdList = wearDegreeService.findAll();
        model.addAttribute("thirdList", wdList);
        // 查询30天最高成交价格
        Double price = phoneService.findPriceLast30Days(id);
        if (price == null){
            model.addAttribute("price", "0");
        }else {
            model.addAttribute("price", price.intValue());
        }
        return "/info";
    }
    @RequestMapping("/price")
    public String calcPrice(Integer id,Integer[] property1,Integer[]property2,Integer[] property3,Model model){
        Integer price = priceService.getPrice(id, property1, property3);
        model.addAttribute("price",price);
        String phoneName = priceService.getPhoneName(id);
        model.addAttribute("name",phoneName);
        String img = priceService.getPhoneImg(id);
        model.addAttribute("img",img);
        return "price";
    }

    @ResponseBody
    @RequestMapping("/findPriceLast4Months")
    public List<LineChartEntity> findPriceLast4Months(Integer id){
        return phoneService.findPriceLast4Months(id);
    }
}